<template>
  <div class="auto">
    <form  class="form-horizontal">
      <p class="bg-info text-center" style="padding: 8px;">变更表参数</p>
      <div class="row">
      <label for="f_settingtype" class="col-sm-2 control-label">变更的参数类型:</label>
      <div class="col-sm-2" >
        <v-select id="f_settingtype"
        v-model="changemodel.f_settings"
        placeholder='选择参数类型'
        :value.sync="changemodel.f_settings"
        :options='settingType'
        close-on-select clear-button>
        </v-select>
      </div>
    </div>
      <div style="height:400px; overflow: auto;margin-top:20px;">
        <run-time-setting v-ref:runtime :data='row' v-if='isruntime'></run-time-setting>
        <net-setting v-ref:net :data='row' v-if='isnet'></net-setting>
        <pay-type-setting  v-ref:paytype :data='row' v-if='ispaytype' ></pay-type-setting>
      </div>
    </form>
    <div style="text-align:right;height:auto;margin-right:50px;">
      <button class="btn btn-success" @click="confirm()" >确&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;认</button>
      <button class="btn btn-default" @click="clean()" style="width:80px;margin-left:20px;">取&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;消</button>
    </div>
  </div>
</template>
<script>
/**
*变更物联网表参数
*/
import * as Util from '../../../../components/Util'
import AppData from '../../../../stores/AppData'
export default {
  title: '表参数设置',
  data () {
    return {
      isruntime: false,
      isnet: false,
      ispaytype: false,
      commonmodel: {},
      settingmodel: {},
      model: {},
      param: {
        f_filiale: Util.f.f_fengongsi,
        f_outlets: Util.f.f_parentname,
        type: '表参数设置'
      },
      changemodel: {
        f_settings: ['']
      }
    }
  },
  props: ['row', 'data'],
  methods: {
    confirm () {
      this.transfertype()
      if (this.row) {
        this.commonmodel = {
          f_user_id: this.row.f_user_id,
          f_userinfo_id: this.row.f_userinfo_id,
          f_meternumber: this.row.f_meternumber
        }
        this.settingmodel = Object.assign({}, this.settingmodel, this.commonmodel)
        this.singleChangeSetting()
      } else {
        this.bulkChange()
      }
    },
    transfertype () {
      if (this.changemodel.f_settings[0] === '运行参数') {
        this.$refs.runtime.converseparam()
        this.model = Object.assign({}, this.$refs.runtime.model)
        this.settingmodel = Object.assign({}, this.$refs.runtime.Settingmodel)
      } else if (this.changemodel.f_settings[0] === '网络参数') {
        this.$refs.net.converseparam()
        this.model = Object.assign({}, this.$refs.net.model)
        this.settingmodel = Object.assign({}, this.$refs.net.Settingmodel)
      } else if (this.changemodel.f_settings[0] === '计费方案') {
        this.$refs.paytype.converseparam()
        this.model = Object.assign({}, this.$refs.paytype.model)
      }
    },
    bulkChange () {
      this.$WebMeterService.bulkChangeSetting(this.model, this.settingmodel, this.data, this.param)
      .then(
        (res) => {
          this.$dispatch('success', '参数下发', this.data, res)
        }
      ).catch(
        (error) => {
          this.$dispatch('error', '参数下发', this.data, error)
        }
      )
    },
    singleChangeSetting () {
      this.$WebMeterService.singleChangeSetting(this.model, this.settingmodel, this.row, this.param)
      .then(
        (res) => {
          this.$dispatch('success', '参数下发', this.data, res)
        }
      ).catch(
        (error) => {
          this.$dispatch('error', '参数下发', this.data, error)
        }
      )
    },
    clean () {
      this.$info('取消操作')
      this.$dispatch('clean', '参数下发')
    }
  },
  watch: {
    'changemodel.f_settings' (val) {
      if (val[0] === '运行参数') {
        this.isruntime = true
        this.isnet = false
        this.ispaytype = false
      } else if (val[0] === '网络参数') {
        this.isruntime = false
        this.isnet = true
        this.ispaytype = false
      } else if (val[0] === '计费方案') {
        this.isruntime = false
        this.isnet = false
        this.ispaytype = true
      }
    }
  },
  computed: {
    settingType () {
      return AppData.getParam('物联网表参数设置')
    }
  }
}
</script>
